﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PT.NoteDetection.Midi;

namespace PT.NoteDetection.Tests.Midi {
    [TestClass]
    public class TimeServiceTest {

        [TestMethod]
        public void CalculateMidiTimeTest() {
            var target = new TimeService();

            var time = new TimeSpan(0, 0, 0, 5);  // 5 s

            // mt = mtps / cps * c = mtps * c / cps, where
            //      MidiTicksPerSecond (mtps) = TicksPerQuarterNote (=480) * Tempo (=90) / 60(s)
            //      mtps = 480 * 90 / 60 = 720 
            //      cps - clicks (TimeSpan ticks) per second, c - clicks (TimeSpan clicks)
            //      
            //      1s = 10 000 000; 5s = 50 000 000 ticks
            //      mt = 720 * 50 000 000 / 10 000 000 = 720 * 5 = 3600
            var expectedMidiTime = 3600L;

            var actualMidiTime = target.CalculateMidiTime(time);
            Assert.AreEqual(expectedMidiTime, actualMidiTime);

            time = new TimeSpan(0, 0, 0, 1, 850);
            expectedMidiTime = 1332L;

            actualMidiTime = target.CalculateMidiTime(time);
            Assert.AreEqual(expectedMidiTime, actualMidiTime);
        }
    }
}
